<?php
/**
 * @annotate 企业设置类
 * @group   Admin/Company
 * @author   yachtxiao <fuck@fackyou.org>
 */
class SettingAction extends BaseAction {
	protected $aVerify = array(
   	'info','saveinfo','license','addlicense','store','doadd_store','edit_store','doedit_store','brand','add_brand',
   	'search_brand','bind_brand','getarea','check_pword','edit_password','pic_list','add_pic','del_pic','del_store','del_brand','wxbind','each_wxbind',
   	'save_wxbind','del_wxbind','diyindex','diyview','set_company_diystyle','set_company_diybg',
   	'set_company_diylogo','set_company_diytool','esc_company_diytool','add_faxian','faxian','apply','add_apply','menu','apply_people'
  	);//需要验证的方法
	public function identify(){
		$this->display();
	}
	
	public function info(){
		$this->assign("Title","基本信息");
		$hangye = gethangye();
		$pro = getarea();
		D('Company')->confirminfo();
		$company_info = D('Company')->getinfo();
		$company_info['company_name'] = $_SESSION['company']['company_name'];
		$this->assign('pro',$pro);
		$this->assign('company',$company_info);
		$this->assign('hangye',$hangye);
		$this->display();
	}
	
	public function saveinfo(){
		$data = $_POST;
		$result = D('Company')->saveinfo($data);
		$this->assign("jumpUrl",U('Setting/info'));
		if($result){
			$this->success("保存成功！",U('Company/index'));
		}else{
			$this->error('保存失败');
		}
	}
	
	public function license(){
		$this->assign("Title","公司认证");
		$license_status = D('Company')->status_license();
		$this->assign('status',$license_status);
		$this->display();
	}
	
	public function addlicense(){
		if($_FILES['license']['name'] == ""){
			$this->error('请选择营业执照上传');
		}else{
			$aid = upload_img_one('license',$_FILES['license']);
		}
		
		if($aid){
			D('Company')->update_license($aid);
			$this->success("上传成功！",U('Company/index'));
		}else{
			$this->error('上传失败');
		}
		$this->assign("jumpUrl",U('Setting/license'));
	}
	
	public function store(){
		$this->assign("Title","门店管理");
		$stores = D('Company')->list_store();
		foreach($stores['data'] as $k => &$v){
			$pro = getareaname($v['pro']);
			$city = getareaname($v['city']);
			$area = getareaname($v['area']);
			$v['areas'] = $pro.$city.$area;
		}
		$pro = getarea();
		$this->assign('pro',$pro);
		$this->assign('stores',$stores);
		$this->display();
	}
		
	public function doadd_store(){
		$result = D('Company')->addstore($_POST);
		if($result){
			$this->success("添加成功！");
		}else{
			$this->error('添加失败');
		}
	}
	
	public function edit_store(){
		$this->assign("Title","修改门店");
		$sid = $_GET['sid'];
		$store = D('Company')->get_store($sid);
		$pro = getarea();
		$this->assign('pro',$pro);
		$this->assign('store',$store);
		$this->display();
	}
	
	public function doedit_store(){
		$_POST['sid'] = $_GET['sid'];
		$result = D('Company')->edit_store($_POST);
		if($result){
			$this->success("修改成功！");
		}else{
			$this->error('修改失败');
		}
	}
	
	public function brand(){
		$this->assign("Title","关联品牌");
		$brand = D('Company')->get_brand();
		$this->assign('brand',$brand);
		$this->display();
	}
	
	public function add_brand(){
		$aid = upload_img_one('brand',$_FILES['brand_ico']);
		$data = array(
					'brand_name' => $_POST['brand_name'],
					'brand_ico' => $aid,
					'brand_intro' => $_POST['brand_intro'],
					'brand_status' => 0,
					'updateline' => time()
				);
		$result = D('Company')->add_brand($data);
		$bid = mysql_insert_id();
		$re = D('Company')->bind_brand($bid);
		if($re){
			$this->success("提交成功！请等待审核！");
		}else{
			$this->error('新增失败');
		}
	}
	
	public function search_brand(){
		$key = $_REQUEST['key'];
		$result = D('Company')->search_brand($key);
		exit(json_encode($result));
	}
	
	public function bind_brand(){
		$bid = $_POST['bid'];
		$result = D('Company')->bind_brand($bid);
		if($result){
			$this->ajaxReturn('','绑定成功',1);
		}else{
			$this->ajaxReturn('','绑定失败',0);
		}
	}

	public function getarea(){
		$type = $_POST['type'];
		$aid = $_POST['aid'];
		$area  = getarea($type,$aid);
		exit(json_encode($area));
	}
	
	public function check_pword(){
		$admin = $_SESSION['company'];
		$pwd = $_POST['pwd'];
		$pwd1 = md5(md5($pwd).$admin['company_salt']);
		if($pwd1 == $admin['company_pwd']){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}

	public function edit_password(){
		$this->assign('Title',"修改密码");
		$cid = $_SESSION['company']['cid'];
		$this->checkPost();
		$pwd = getRequest('pwd2');
		$da = D('Company')->edit_pwd($cid,$pwd);
		$url = U('Login/sign_in');
		if($da){
			$this->assign("jumpUrl",$url);//跳转
			$this->success("修改成功！请重新登录！");      	
		}
	}
	public function pic_list(){
		 $pic = D('Company_pic')->list_pic();
		 $this->assign('list',$pic);
		 $this->display();
	}
	public function add_pic(){
		$cid = $_SESSION['company']['cid'];
		$aid = upload_img_one('company',$_FILES['fileToUpload']);
		$data = array(
				'fid' =>$aid,
				'cid' =>$cid,
				'ctime'=>time()
			);
		$cou = D('company_pic')->where(array('cid'=>$cid,'is_del'=>0))->count();
		if($cou>=6){
			$this->error('您公司的图片已经达到六张了，请删除一部分，再进行上传！');
		}else{
			$da = D('company_pic')->add($data);
			if($da){
			$this->success('上传成功！');
			}else{
				$this->error('上传失败！');
			}
		}
	}
	public function del_pic(){
		$map['pid'] = $_POST['pid'];
		$data['is_del'] = 1;
		$da = D('company_pic')->where($map)->save($data);
		if($da){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	public function del_store(){
		$map['sid'] = $_POST['sid'];
		$data['is_del'] = 1;
		$da = D('store')->where($map)->save($data);
		if($da){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	public function del_brand(){
		$bid = $_POST['bid'];
		$cid = $_SESSION['company']['cid'];
		$brand = D('company_info')->where('cid = '.$cid)->find();
		$brands = explode(',',$brand['brand']);
		$key = array_search($bid,$brands);
		unset($brands[$key]);
		$data['brand'] = implode(',',$brands);
		$result = D('company_info')->where('cid = '.$cid)->save($data);
		if($result){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	
	public function wxbind(){
		$qrcode = D('Weixin')->get_wxbind_qrcode();
		$qrcodename = explode('wx_',$qrcode);
		$code = explode('.',$qrcodename[1]);
		$manage = D('Company')->get_manage_list();
		$this->assign('manage',$manage);
		$this->assign('qrcode',$qrcode);
		$this->assign('code',$code[0]);
		$this->display();
	}
	
	public function each_wxbind(){
		$code = $_POST['code'];
		$result = D('Weixin')->get_wxbind_result($code);
		exit(json_encode($result));
	}
	
	public function save_wxbind(){
		$data = $_POST;
		$result = D('Weixin')->save_wxbind($data);
		if($result){
			$this->success("修改成功",'__URL__/wxbind');
		}else{
			$this->error('修改失败','__URL__/wxbind');
		}
	}
	
	public function del_wxbind(){
		$bid = $_POST['bid'];
		$result = D('Weixin')->del_wxbind($bid);
		if($result){
			$this->ajaxReturn('','取消绑定成功',1);
		}else{
			$this->ajaxReturn('','取消绑定失败',0);
		}
	}
	
	public function diyindex(){
		$diyinfo = D('Company')->get_company_diyinfo();
		$this->assign('tool',$diyinfo['tool']);
		$menu = D('Company_menu')->select_menu();
		$this->assign('diy_portal',$menu['diy_portal']);
		$this->display();
	}
	
	public function diyview(){
		$diyinfo = D('Company')->get_company_diyinfo();
		$this->assign('diyinfo',$diyinfo);
		$this->assign('d10',"10");
		$this->assign('d11',"11");
		$this->assign('d12',"12");
		$this->assign('d14',"14");
		$this->assign('d15',"15");
		$this->display();
	}
	
	public function set_company_diystyle(){
		$style = $_POST['style'];
		$result = D('Company')->set_company_diystyle($style);
		if($result){
			$this->ajaxReturn('','设置风格成功',1);
		}else{
			$this->ajaxReturn('','设置风格失败',0);
		}
	}
	
	public function set_company_diybg(){
		$aid = upload_img_one('diybg',$_FILES['fileToUpload']);
		$result = D('Company')->set_company_diybg($aid);
		if($result){
			$this->success("修改成功",'__URL__/diyindex');
		}else{
			$this->error('修改失败','__URL__/diyindex');
		}
	}
	
	public function set_company_diylogo(){
		$aid = upload_img_one('diylogo',$_FILES['fileToUpload']);
		$result = D('Company')->set_company_diylogo($aid);
		if($result){
			$this->success("修改成功",'__URL__/diyindex');
		}else{
			$this->error('修改失败','__URL__/diyindex');
		}
	}
	
	public function set_company_diytool(){
		$toolid = $_POST['toolid'];
		$result = D('Company')->set_company_diytool($toolid);
		if($result){
			$this->ajaxReturn('','添加功能成功',1);
		}else{
			$this->ajaxReturn('','添加功能失败',0);
		}
	}
	
	public function esc_company_diytool(){
		$toolid = $_POST['toolid'];
		$result = D('Company')->esc_company_diytool($toolid);
		if($result){
			$this->ajaxReturn('','取消功能成功',1);
		}else{
			$this->ajaxReturn('','取消功能失败',0);
		}
	}
	
	public function add_faxian(){
		$fid = $_GET['fid'];
		if($fid !=""){
			$fa = D('company_faxian')->where(array('fid'=>$fid))->find();
			$fa['pic'] = get_img_url($fa['pic']);
			$this->assign('list',$fa);
		}
		if ($this->isPost()){
			$cid = $_SESSION['company']['cid'];
			if($_FILES['fileToUpload']['name'] != ""){
				$aid = upload_img_one('company',$_FILES['fileToUpload']);
				$data['pic'] = $aid;
			}
			
			$data['cid'] = $cid; 
			$data['title'] = $_POST['title'];
			$data['text'] = $_POST['text'];
			$data['ctime'] = time();
		
			$id = $_POST['id'];
			if($id == ""){
				$result = D('company_faxian')->add($data);
			}else{
				$result = D('company_faxian')->where(array('fid'=>$id))->save($data);
			}
			
			if($result){
				$this->success('提交成功！',"__URL__/faxian");
			}else{
				$this->error('提交失败！');
			}
		}
		$this->display();
	}	

	public function add_apply(){
		$aid = $_GET['aid'];
		if($aid !=""){
			$fa = D('company_apply')->where(array('aid'=>$aid))->find();
			$fa['logo'] = get_img_url($fa['logo']);
			$this->assign('list',$fa);
		}
		if ($this->isPost()){
			$cid = $_SESSION['company']['cid'];
			if($_FILES['fileToUpload']['name'] != ""){
				$aid = upload_img_one('company',$_FILES['fileToUpload']);
				$data['logo'] = $aid;
			}
			
			$data['cid'] = $cid; 
			$data['title'] = $_POST['title'];
			$data['text'] = $_POST['text'];
			$data['ctime'] = time();
		
			$id = $_POST['id'];
			if($id == ""){
				$result = D('company_apply')->add($data);
			}else{
				$result = D('company_apply')->where(array('aid'=>$id))->save($data);
			}
			
			if($result){
				$this->Yes = 1;
		        $this->Suce = "提交成功！";
		        $this->Jurl = "__URL__/apply";
				//$this->success('提交成功！',"__URL__/faxian");
			}else{
				$this->Yes = "-1";
	        	$this->Err = "提交失败！";
				//$this->error('提交失败！');
			}
		}
		$this->display();
	}	

	public function faxian(){
		$cid = $_SESSION['company']['cid'];
		$list = D('company_faxian')->where(array('cid'=>$cid,'is_del'=>0))->order('ctime desc')->findpage(20);
		$this->assign('list',$list);
		$this->display();
	}
	public function apply(){
		$cid = $_SESSION['company']['cid'];
		$list = D('company_apply')->where(array('cid'=>$cid,'is_del'=>0))->order('ctime desc')->findpage(20);
		foreach ($list['data'] as $k => &$v) {
			$v['people'] = D('company_apply_people')->where('aid ='.$v['aid'])->count();
		}
		$this->assign('list',$list);
		$this->display();
	}
	public function apply_people(){
		$aid = $_GET['aid'];
		$list = D('company_apply_people')->where(array('aid'=>$aid))->order('ctime desc')->findpage(20);
		foreach ($list['data'] as $k => &$v) {
			$v['title'] = D('company_apply')->where('aid ='.$v['aid'])->getField('title');
		}
		$this->assign('list',$list);
		$this->display();
	}
	public function  del_faxian(){
		$map['fid'] = $_POST['id'];
		$data['is_del'] = 1;
		$da = D('company_faxian')->where($map)->save($data);
		if($da){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	public function  del_apply(){
		$map['aid'] = $_POST['id'];
		$data['is_del'] = 1;
		$da = D('company_apply')->where($map)->save($data);
		if($da){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	public function  del_apply_people(){
		$map['pid'] = $_POST['id'];
		$data['is_del'] = 1;
		$da = D('company_apply_people')->where($map)->save($data);
		if($da){
			$this->ajaxReturn('','success',1);
		}else{
			$this->ajaxReturn('','error',0);
		}
	}
	public function menu(){
		$cid = $_SESSION['company']['cid'];
		$menu = D('company_menu')->where('cid ='.$cid)->find();
		if($menu['mid'] == 2){
			$menu['endtime'] = strtotime('+1 month',$menu['ctime']);
		}else if($menu['mid'] == 4){
			$menu['endtime'] = strtotime('+3 month',$menu['ctime']);
		}else if($menu['mid'] == 5){
			$menu['endtime'] = strtotime('+1 year',$menu['ctime']);
		}
		$this->assign('menu',$menu);
		$this->display();
	}
}
?>